package com.hulk.ratel.manage.commoncache;

import cn.hutool.core.bean.BeanUtil;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hulk.ratel.common.constant.enums.ErrFlag;
import com.hulk.ratel.common.constant.enums.UseFlag;

import com.hulk.ratel.manage.bo.ErrcodeConvertBO;
import com.hulk.ratel.persistence.entity.ErrcodeConvert;
import com.hulk.ratel.persistence.repository.IErrcodeConvertRepository;
import com.vip.vjtools.vjkit.mapper.BeanMapper;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * Created by hulk on 2017/11/22.
 * 【参数信息】机构合作方应答码转换表
 */
@Slf4j
@Component
public class ErrCodeConvertCache {

    @Autowired
    private IErrcodeConvertRepository repository;

    /**
     * 查询机构合作方应答码转换
     *
     * @param upOrgCode
     * @param respCode  机构返回错误码
     * @return
     */
    @Cached(key = "#upOrgCode + #respCode + 'ErrCodeConvertCache_findErrCodeConvert'", cacheType = CacheType.LOCAL ,expire =600)
    public ErrcodeConvertBO findErrCodeConvert(@NonNull String upOrgCode, @NonNull String respCode) {


        ErrcodeConvert  convert =  new ErrcodeConvert();
        convert.setRespCode(respCode);
        convert.setOrgCode(upOrgCode);
        convert.setUseFlag(UseFlag.Y.getValue());
        convert.setErrFlag(ErrFlag.Y.getValue());
        Wrapper<ErrcodeConvert> query = new QueryWrapper<>(convert);
        List<ErrcodeConvert> opt = repository.list(query);

        if(opt==null|opt.isEmpty()){
            return null;
        }else{
            convert  =opt.get(0) ;
            ErrcodeConvertBO pbo = new ErrcodeConvertBO();
            BeanUtil.copyProperties(convert, pbo);
            return pbo;
        }


    }
}
